<?php
define('LIKE', 'LIKE');

$connectionInfo = array("UID"=>$dbuser, "PWD"=>$dbpassword, "Database"=>$DefaultCompany, "CharacterSet"=>"UTF-8");
$db = sqlsrv_connect($host, $connectionInfo);

if ($db === false) {
    echo "Unable to connect.</br>";
    die(print_r(sqlsrv_errors(), true));
}

function DB_last_insert_id(&$Conn) {
    $id = 0;
    $res = sqlsrv_query( $Conn, "SELECT @@identity AS id");
    if ($row = sqlsrv_fetch_array($res, SQLSRV_FETCH_ASSOC)) {
        $id = $row["id"];
    }
    return $id;
}

function DB_query($SQL, &$Conn, $ErrorMessage = '', $DebugMessage = '', $Transaction = false, $TrapErrors = true) {

    global $debug;
    global $PathPrefix;
    $result = sqlsrv_query($Conn, $SQL);
    $_SESSION['LastInsertId'] = DB_last_insert_id($Conn);
    if ($DebugMessage == '') {
        $DebugMessage = _('The SQL that failed was');
    }
    
    if ($result === false AND $TrapErrors == true) {
        /*
         if ($TrapErrors){
         require_once($PathPrefix . 'includes/header.inc');
         }
         prnMsg($ErrorMessage.'<br />' . DB_error_msg($Conn),'error', _('Database Error'));
         if ($debug==1){
         prnMsg($DebugMessage. "<br />$SQL<br />",'error',_('Database SQL Failure'));
         }
         if ($Transaction){
         $SQL = 'rollback';
         $Result = DB_query($SQL,$Conn);
         if (DB_error_no($Conn) !=0){
         prnMsg(_('Error Rolling Back Transaction'), '', _('Database Rollback Error') );
         }
         }
         if ($TrapErrors){
         include($PathPrefix . 'includes/footer.inc');
         exit;
         }*/
    } elseif (isset($_SESSION['MonthsAuditTrail']) and isset($result) AND $_SESSION['MonthsAuditTrail'] > 0) {
    
        $SQLArray = explode(' ', $SQL);
        
        if (($SQLArray[0] == 'INSERT') OR ($SQLArray[0] == 'UPDATE') OR ($SQLArray[0] == 'DELETE')) {
        
            if ($SQLArray[2] != 'audittrail') { // to ensure the auto delete of audit trail history is not logged
                $AuditSQL = "INSERT INTO audittrail (transactiondate,
									userid,
									querystring)
						VALUES(N'".Date('Y-m-d H:i:s')."',
							N'".trim($_SESSION['UserID'])."',
							'".str_replace("'", '', $SQL)."')";
							
                //$AuditResult = sqlsrv_query($Conn, $AuditSQL);
            }
        }
    }
    return $result;
    
}

function DB_escape_string($String) {
    return addslashes($String);
}

function DB_num_rows(&$ResultIndex) {
    return sqlsrv_num_rows($ResultIndex);
}

function DB_has_rows(&$ResultIndex) {
    return sqlsrv_has_rows($ResultIndex);
}

function DB_fetch_object($ResultIndex) {
    return sqlsrv_fetch_object($ResultIndex);
}

function DB_fetch(&$ResultIndex, $option) {

    $RowPointer = sqlsrv_fetch($ResultIndex, $option);
    Return $RowPointer;
}

function DB_free_result(&$ResultIndex) {
    sqlsrv_free_stmt($ResultIndex);
}

function DB_get_field(&$ResultIndex, $index) {
    return sqlsrv_get_field($ResultIndex, $index);
}

function DB_fetch_array(&$ResultIndex, $Option = SQLSRV_FETCH_ASSOC) {

    $RowPointer = sqlsrv_fetch_array($ResultIndex, $Option);
    Return $RowPointer;
}
?>